import {dataURLtoFile} from '@/utils/64batatoFile'
import request from '@/utils/request'
import lrz from 'lrz'
import html2Canvas from 'html2canvas'
import domtoimage from 'dom-to-image';
export function upFile(name,data,domid) {
    let formData = new FormData
    if(data){
        Object.keys(data).map(item=>{
            formData.append(item,data[item])
        })
    }
    let nodeid = document.querySelector('#'+domid)
    let  baseUrl=process.env.NODE_ENV === 'development' ? process.env.VUE_APP_BASE_API :window.location.href.slice(0, window.location.href.indexOf('/user'))
    return new Promise((resolve,reject)=>{
        domtoimage.toPng(nodeid).then(function (dataUrl) {
            // let pageData = canvas.toDataURL('image/jpeg', 1.0)
            lrz(dataUrl).then(res=>{
                let obj = dataURLtoFile(res.base64,name+'.jpg')
                // let list = [obj]
                formData.append('img',obj)
                request(
                    {
                        url:baseUrl+'/admin/MemberApply/upload',
                        method:'post',
                        data: formData
                    }
                ).then(res=>{
                    resolve(res)
                }).catch((err)=>{
                    reject(err)
                })
            })
        })
    })
}
export function upFileqm(imgUrl,name,data) {
    let formData = new FormData
    if(data){
        Object.keys(data).map(item=>{
            formData.append(item,data[item])
        })
    }
    let  baseUrl=process.env.NODE_ENV === 'development' ? process.env.VUE_APP_BASE_API :window.location.href.slice(0, window.location.href.indexOf('/user'))
    return new Promise((resolve,reject)=>{
        lrz(imgUrl).then(res=>{
            let obj = dataURLtoFile(res.base64,name+'.jpg')
            // let list = [obj]
            formData.append('img',obj)
            request(
                {
                    url:baseUrl+'/admin/MemberApply/upload',
                    method:'post',
                    data: formData
                }
            ).then(res=>{
                resolve(res)
            }).catch((err)=>{
                reject(err)
            })
        })
    })
}